package com.chb.service.impl;

import com.chb.dao.GoodsDao;
import com.chb.dao.StaffDao;
import com.chb.model.entity.*;
import com.chb.model.vo.BranchAllGoodsVo;
import com.chb.model.vo.BranchGoodsVo;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ：njh
 * @date ：Created in 2023/9/26 15:57
 * @description：商品模块实现类
 * @modified By：
 * @version:
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private HttpSession session;

    @Override
    public Result findAllGoods(Integer currentPage, Integer pageSize, Integer staffBranchid) {
        //获取员工id,分店id
        Staff staff = (Staff) session.getAttribute("staff");
        staffBranchid = staff.getBranchid();

        if (staffBranchid == 12138) {
            staffBranchid = null;
        }

        PageHelper.startPage(currentPage, pageSize);
        List<BranchGoodsVo> list = goodsDao.findAllGoods(staffBranchid);
        PageInfo<BranchGoodsVo> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }

    @Override
    public Integer addGoods(Goods goods) {
        Integer row = goodsDao.addGoods(goods);
        return row;
    }

    @Override
    public Integer delBranchGoods(Integer id) {
        Integer row = goodsDao.delBranchGoods(id);
        return row;
    }

    @Override
    public Integer updGoods(Goods goods) {
        Integer row = goodsDao.updGoods(goods);
        return row;
    }

    @Override
    public Result searchGoods(Integer currentPage, Integer pageSize, String goodsname, Integer keycapid, Integer keyboardcolor, Integer shaftchooseid, Integer backlightcolorid, Integer branchid) {

        //获取员工id,分店id
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getBranchid() != 12138 && branchid == null) {
            branchid = staff.getBranchid();
        }

        PageHelper.startPage(currentPage, pageSize);
        List<BranchGoodsVo> list = goodsDao.searchGoods(goodsname, keycapid, keyboardcolor, shaftchooseid, backlightcolorid, branchid);
        PageInfo<BranchGoodsVo> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }


    @Override
    public Result findAllKeyBoardColor(Integer currentPage, Integer pageSize) {

        PageHelper.startPage(currentPage, pageSize);
        List<KeyBoardColor> allKeyBoardColor = goodsDao.findAllKeyBoardColor();
        PageInfo<KeyBoardColor> pageInfo = new PageInfo<>(allKeyBoardColor);

        return Result.success(pageInfo);
    }

    @Override
    public Integer addKeyBoardColor(KeyBoardColor keyBoardColor) {
        Integer row = goodsDao.addKeyBoardColor(keyBoardColor);
        return row;
    }

    @Override
    public Integer delKeyBoardColor(Integer keyboardcolorid) {
        Integer row = goodsDao.delKeyBoardColor(keyboardcolorid);
        return row;
    }

    @Override
    public Integer updKeyBoardColor(KeyBoardColor keyBoardColor) {
        Integer update = goodsDao.updKeyBoardColor(keyBoardColor);
        return update;
    }

    @Override
    public Result searchKeyBoardColor(Integer currentPage, Integer pageSize, Integer keyboardcolorid, String keyboardcolorname) {

        if(currentPage!=null&&pageSize!=null){
            PageHelper.startPage(currentPage, pageSize);
            List<KeyBoardColor> list = goodsDao.searchKeyBoardColor(keyboardcolorid, keyboardcolorname);
            PageInfo<KeyBoardColor> pageInfo = new PageInfo<>(list);

            return Result.success(pageInfo);
        }else{
            List<KeyBoardColor> list = goodsDao.searchKeyBoardColor(keyboardcolorid, keyboardcolorname);
            return Result.success(list);
        }



    }


    @Override
    public Result findAllShaftChoose(Integer currentPage, Integer pageSize) {

        PageHelper.startPage(currentPage, pageSize);
        List<ShaftChoose> list = goodsDao.findAllShaftChoose();
        PageInfo<ShaftChoose> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }

    @Override
    public Integer addShaftChoose(ShaftChoose shaftChoose) {
        Integer row = goodsDao.addShaftChoose(shaftChoose);
        return row;
    }

    @Override
    public Integer delShaftChoose(Integer shaftchooseid) {
        Integer row = goodsDao.delShaftChoose(shaftchooseid);
        return row;
    }

    @Override
    public Integer updShaftChoose(ShaftChoose shaftChoose) {
        Integer update = goodsDao.updShaftChoose(shaftChoose);
        return update;
    }

    @Override
    public Result searchShaftChoose(Integer currentPage, Integer pageSize, Integer shaftchooseid, String shaftchoose) {

        if(currentPage!=null&&pageSize!=null){
            PageHelper.startPage(currentPage, pageSize);
            List<ShaftChoose> list = goodsDao.searchShaftChoose(shaftchooseid, shaftchoose);
            PageInfo<ShaftChoose> pageInfo = new PageInfo<>(list);

            return Result.success(pageInfo);
        }else{
            List<ShaftChoose> list = goodsDao.searchShaftChoose(shaftchooseid, shaftchoose);
            return Result.success(list);
        }

    }


    @Override
    public Result findAllBackLightColor(Integer currentPage, Integer pageSize) {

        PageHelper.startPage(currentPage, pageSize);
        List<BackLightColor> list = goodsDao.findAllBackLightColor();
        PageInfo<BackLightColor> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo);
    }

    @Override
    public Integer addBackLightColor(BackLightColor backLightColor) {
        Integer row = goodsDao.addBackLightColor(backLightColor);
        return row;
    }

    @Override
    public Integer delBackLightColor(Integer backlightcolorid) {
        Integer row = goodsDao.delBackLightColor(backlightcolorid);
        return row;
    }

    @Override
    public Integer updBackLightColor(BackLightColor backLightColor) {
        Integer update = goodsDao.updBackLightColor(backLightColor);
        return update;
    }

    @Override
    public Result searchBackLightColor(Integer currentPage, Integer pageSize, Integer backlightcolorid, String backlightcolorname) {

        if(currentPage!=null&&pageSize!=null){
            PageHelper.startPage(currentPage, pageSize);
            List<BackLightColor> list = goodsDao.searchBackLightColor(backlightcolorid, backlightcolorname);
            PageInfo<BackLightColor> pageInfo = new PageInfo<>(list);

            return Result.success(pageInfo);
        }else{
            List<BackLightColor> list = goodsDao.searchBackLightColor(backlightcolorid, backlightcolorname);
            return Result.success(list);
        }

    }

    @Override
    public Result goodsList(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<BranchAllGoodsVo> allGoodsVos = goodsDao.selectAllGoodsInfo();
        PageInfo pageInfo = new PageInfo<>(allGoodsVos);
        PageBean pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }

    @Override
    public List<KeyCap> searchKeyCap() {
        List<KeyCap> keyCapName = goodsDao.searchKeyCap();
        return keyCapName;
    }
}